package com.zkr.subclass.service;


import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.subclass.mapper.SubClassMapper;
import com.zkr.subclass.model.*;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2020/12/24.
 */
@Service
public class SubClassServiceimpl implements SubClassService {

    @Autowired
    private SubClassMapper subClassMapper;

    @Override
    public ResultVo queryPage(SubClassSelectVo subClassSelectVo) {

         int count = subClassMapper.queryCount(subClassSelectVo);
         List<SubClassVo> subClassVoList = subClassMapper.queryPage(subClassSelectVo);
         for (SubClassVo subClassVo:subClassVoList){
             /*if(subClassVo.getStatus()==true){
                 subClassVo.setStatusName("启用");
             }else  if(subClassVo.getStatus()==false){
                 subClassVo.setStatusName("禁用");
             }*/
            Integer status = subClassVo.getStatus()==true?1:0;
             for (StatusEnum c:StatusEnum.values()) {
                 if (c.getValue().equals(status)){
                     subClassVo.setStatusName(c.getName());
                 }
             }
         }

        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(subClassSelectVo.getPageIndex());
        allRecords.setDataList(subClassVoList);
        allRecords.setPageSize(subClassSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);


    }

    @Override
    @Transactional
    public ResultVo add(@Valid SubClassAddVo subClassAddVo) {
        //验证是否已经存在编码或者名称
        SubClassVo subClassVoName = subClassMapper.queryByName(subClassAddVo.getName());
        if (subClassVoName !=null){
            return ResultBuilderVo.error("备件名称已经存在");
        }
        SubClassVo subClassVoCode = subClassMapper.queryByCode(subClassAddVo.getCode());
        if(subClassVoCode !=null){
            return ResultBuilderVo.error("备件编码已经存在");
        }
        SubClassVo subClassVo = new SubClassVo();
        subClassVo.setCreateName("张三");
        subClassVo.setCreateTime(new Date());
        subClassVo.setUpdateName("张三");
        subClassVo.setUpdateTime(new Date());

        //把左边的数据赋值给右边
        BeanUtils.copyProperties(subClassAddVo,subClassVo);
        //入库
        subClassMapper.insertSubClass(subClassVo);
        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo queryById(Integer id) {

       SubClassVo subClassVo =  subClassMapper.queryById(id);
        if (subClassVo==null){
            return ResultBuilderVo.error("查看的数据不存在!");
        }
       /*if (subClassVo.getStatus()==true){
            subClassVo.setStatusName("启用");
        }else if (subClassVo.getStatus()==false){
            subClassVo.setStatusName("禁用");
        }*/
        for (StatusEnum c:StatusEnum.values()) {
            if (c.getValue().equals(subClassVo.getStatus())){
                subClassVo.setStatusName(c.getName());
            }
        }

        return ResultBuilderVo.success(subClassVo);
    }

    @Override
    @Transactional
    public ResultVo update(@Valid SubClassUpdateVo subClassUpdateVo) {
        //1.名称不能重复
        //2.只有禁用状态才能修改数据
        //3.修改数据库
        SubClassVo subClassVoName = subClassMapper.queryByName(subClassUpdateVo.getName());
        if (subClassVoName != null){
            if (!subClassVoName.getId().equals(subClassUpdateVo.getId())){
                return ResultBuilderVo.error("备件名称已经存在");
            }

        }
        SubClassVo subClassVo = subClassMapper.queryById(subClassUpdateVo.getId());
        if (subClassVo.getStatus()==true){
            return ResultBuilderVo.error("修改时必须为禁用状态");
        }


        SubClassVo subClassVo1 = new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateVo,subClassVo1);
        subClassVo1.setUpdateTime(new Date());
        subClassVo1.setUpdateName("李四");
        int update = subClassMapper.update(subClassVo1);
        if (update==0){
            return ResultBuilderVo.error("数据已被修改,清刷新重试!");
        }
         return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid SubClassUpdateStatusVo subClassUpdateStatusVo) {

        SubClassVo subClassVo = subClassMapper.queryById(subClassUpdateStatusVo.getId());
        if (subClassVo==null){
            return ResultBuilderVo.error("修改数据不存在!");
        }
        SubClassVo subClassVo1 = new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateStatusVo,subClassVo1);
        subClassVo1.setUpdateTime(new Date());
        subClassVo1.setUpdateName("李四");
        subClassMapper.updateStatus(subClassVo1);
        return ResultBuilderVo.success();

    }



}
